package com.yykj.drup.server.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yykj.common.service.RedisService;
import com.yykj.drup.server.mapper.DoctorProductDao;
import com.yykj.drup.server.mapper.ProductDao;
import com.yykj.drup.server.model.DoctorProduct;
import com.yykj.drup.server.model.Product;
import com.yykj.drup.server.service.DoctorProductService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zengg
 * @since 2022-03-14
 */
@Service
@AllArgsConstructor
public class DoctorProductServiceImp extends ServiceImpl<DoctorProductDao, DoctorProduct> implements DoctorProductService {

    final DoctorProductDao doctorProductDao;
    final RedisService redisService;
    final ProductDao productDao;

    @Override
    public void setDoctorProductCount(String doctorId,Integer categoryType ,List<Integer> productIds) {

        productIds.stream().forEach(productId->{
            Integer i = 1 ;
            //根据医生ID，药品类别，药品ID 查询
            DoctorProduct doctorProduct = this.findByDoctorIdAndProductId(doctorId, productId);
            //存在更新，不存在插入
            if(doctorProduct != null){
                doctorProduct.setCountNum(doctorProduct.getCountNum() + i);
                this.updateById(doctorProduct);
            }else {
                doctorProduct =  DoctorProduct
                        .builder()
                        .doctorId(doctorId)
                        .productId(productId)
                        .categoryType(categoryType)
                        .build();
                this.save(doctorProduct);
            }
        });
    }

    @Override
    public DoctorProduct findByDoctorIdAndProductId(String doctorId, Integer productId) {
        LambdaQueryWrapper<DoctorProduct> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper
                .eq(DoctorProduct::getDoctorId,doctorId)
                .eq(DoctorProduct::getProductId,productId);
        return doctorProductDao.selectOne(lambdaQueryWrapper);
    }

    @Override
    public List<Product> findProductsByDoctorId(String doctorId, Integer categoryType) {
        return doctorProductDao.findProductsByDoctorId(doctorId,categoryType);
    }

}
